/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.config.web.login;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.config.entity.login.ConfigLoginWechat;
import com.thinkgem.jeesite.modules.config.service.login.ConfigLoginWechatService;

/**
 * 微信登录配置Controller
 * @author JiangYongKang
 * @version 2016-12-14
 */
@Controller
@RequestMapping(value = "${adminPath}/config/login/configLoginWechat")
public class ConfigLoginWechatController extends BaseController {

	@Autowired
	private ConfigLoginWechatService configLoginWechatService;
	
	@ModelAttribute
	public ConfigLoginWechat get(@RequestParam(required=false) String id) {
		ConfigLoginWechat entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = configLoginWechatService.get(id);
		}
		if (entity == null){
			entity = new ConfigLoginWechat();
		}
		return entity;
	}
	
	@RequiresPermissions("config:login:configLoginWechat:view")
	@RequestMapping(value = {"list", ""})
	public String list(ConfigLoginWechat configLoginWechat, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<ConfigLoginWechat> page = configLoginWechatService.findPage(new Page<ConfigLoginWechat>(request, response), configLoginWechat); 
		model.addAttribute("page", page);
		return "modules/config/login/configLoginWechatList";
	}

	@RequiresPermissions("config:login:configLoginWechat:view")
	@RequestMapping(value = "form")
	public String form(ConfigLoginWechat configLoginWechat, Model model) {
		model.addAttribute("configLoginWechat", configLoginWechat);
		return "modules/config/login/configLoginWechatForm";
	}

	@RequiresPermissions("config:login:configLoginWechat:edit")
	@RequestMapping(value = "save")
	public String save(ConfigLoginWechat configLoginWechat, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, configLoginWechat)){
			return form(configLoginWechat, model);
		}
		configLoginWechatService.save(configLoginWechat);
		addMessage(redirectAttributes, "保存微信登录配置成功");
		return "redirect:"+Global.getAdminPath()+"/config/login/configLoginWechat/?repage";
	}
	
	@RequiresPermissions("config:login:configLoginWechat:edit")
	@RequestMapping(value = "delete")
	public String delete(ConfigLoginWechat configLoginWechat, RedirectAttributes redirectAttributes) {
		configLoginWechatService.delete(configLoginWechat);
		addMessage(redirectAttributes, "删除微信登录配置成功");
		return "redirect:"+Global.getAdminPath()+"/config/login/configLoginWechat/?repage";
	}

}